// carmi/pages/index/index.js
var common = require("../../../common/common.js").common;

var util = require("../../../utils/util.js");

var WxParse = require("../../../wxParse/wxParse.js");

var colorParse = require("../../../common/color.js");

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        payBtn: false,
        crami_name: "",
        autoplay: true,
        activity_id: "",
        details: [],
        isShow: false,
        tmplIds: [],
        //模板消息tmplIds
        subscribeMessage: true
    },
    //获取去右侧胶囊的信息
    getHeader() {
        let menuRect = wx.getMenuButtonBoundingClientRect();
        this.setData({
            topBarHeight: menuRect.height,
            topBarTop: menuRect.top
        });
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.getHeader();
        wx.hideShareMenu();
        if (options.status) {
            this.setData({
                status: Number(options.status)
            });
        }
        this.getBase();
        // this.getAcitivity();
                this.getTmplIds();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        wx.stopPullDownRefresh();
        this.getBase();
        // this.getAcitivity();
                this.getTmplIds();
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    //全局基础配置信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                wx.setNavigationBarTitle({
                    title: res.extra.title
                });
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: res.extra.color_main
                });
                that.setData({
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    apptitle: res.extra.title,
                    style: res.extra.style,
                    leader_change: res.extra.l_change,
                    leader_express: res.extra.l_express,
                    rp_name: res.extra.rp_name,
                    order_start: res.extra.order_start,
                    order_start_switch: res.extra.order_start_switch,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    communityName: res.extra.community_title ? res.extra.community_title : "社区",
                    leader_address_on: res.extra.leader_address_on
                });
                //处理模块开关
                                var features = common.getModulesSwich(res.extra.features);
                //处理数据格式
                                that.setData({
                    features: features
                });
                //设置模块颜色
                                var componentStyle = that.data.componentStyle;
                that.setData({
                    componentStyle: {
                        ...componentStyle,
                        color_main: !res.extra.color_main ? "#FE513B" : res.extra.color_main,
                        color_minor: !res.extra.color_minor ? "#FE513B" : res.extra.color_minor
                    }
                });
            }
        });
    },
    search_carm() {
        let that = this;
        common.request({
            endpoint: "Exchange/exchange/get-activity-by-code",
            method: "post",
            data: {
                code: that.data.crami_name,
                model: that.data.status
            },
            success: res => {
                if (res.code == 0) {
                    that.setData({
                        isShow: true,
                        activity_id: res.extra.activity_id,
                        send_message: res.extra.send_message,
                        details: res.extra.img,
                        rule: WxParse.wxParse("rule", "html", res.extra.rule, that, 0)
                    });
                } else {
                    wx.showModal({
                        title: "提示",
                        content: res.message
                    });
                }
            },
            fail: res => {
                that.setData({
                    isShow: false,
                    activity_id: "",
                    send_message: "",
                    details: [],
                    rule: ""
                });
                wx.showModal({
                    title: "提示",
                    content: res.message
                });
            }
        });
    },
    gotoUrl: function(e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        });
    },
    //记录搜索名称
    saveWord: function(e) {
        var that = this;
        var crami_name = e.detail.value;
        that.setData({
            crami_name: crami_name
        });
    },
    pay: function() {
        let that = this;
        if (!that.data.crami_name) {
            wx.showModal({
                title: "提示",
                content: "请输入兑换码!",
                showCancel: false
            });
            return false;
        }
        common.request({
            endpoint: "Exchange/exchange/exchange",
            method: "post",
            header: {},
            data: {
                code: that.data.crami_name,
                activity_id: that.data.activity_id,
                model: that.data.status
            },
            success: function(res) {
                let info = res.extra;
                info.status = that.data.status;
                that.setData({
                    info: info
                });
                if (that.data.subscribeMessage && that.data.send_message == 1) {
                    wx.requestSubscribeMessage({
                        tmplIds: that.data.tmplIds,
                        success(res) {},
                        fail(err) {
                            console.log(err);
                        },
                        complete() {
                            wx.navigateTo({
                                url: "/carmi/pages/suc/suc?info=" + encodeURIComponent(JSON.stringify(that.data.info))
                            });
                        }
                    });
                } else {
                    wx.navigateTo({
                        url: "/carmi/pages/suc/suc?info=" + encodeURIComponent(JSON.stringify(that.data.info))
                    });
                }
            },
            fail(res) {
                wx.showModal({
                    title: "提示",
                    content: res.message,
                    showCancel: false
                });
            }
        });
    },
    swiperChange(e) {
        this.setData({
            currentIndex: e.detail.current
        });
    },
    getAcitivity: function() {
        let that = this;
        common.request({
            endpoint: "Exchange/exchange/activity",
            method: "post",
            header: {},
            data: {
                model: that.data.status
            },
            success: function(res) {
                that.setData({
                    activity_id: res.extra.activity_id,
                    send_message: res.extra.send_message,
                    details: res.extra.img,
                    rule: WxParse.wxParse("rule", "html", res.extra.rule, that, 0)
                });
            },
            fail(err) {
                wx.showModal({
                    title: "提示",
                    content: err.message,
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            wx.navigateBack({});
                        }
                    }
                });
            }
        });
    },
    //获取tmplIds
    getTmplIds: function() {
        let that = this;
        const version = wx.getSystemInfoSync().SDKVersion;
        //console.log(version);
                if (util.compareVersion(version, "2.8.2") < 0) {
            that.setData({
                tmplIds: [],
                subscribeMessage: false
            });
            return;
        }
        common.request({
            endpoint: "set/template_get",
            method: "post",
            data: {},
            header: {
                "content-type": "application/json"
            },
            success: res => {
                console.log(res);
                let tmplIds = [];
                let subscribeMessage = true;
                res.extra.map(v => {
                    if (v.type == "30" && v.status == "1") {
                        tmplIds.push(v.template_id);
                    }
                });
                if (!tmplIds.length) {
                    subscribeMessage = false;
                }
                that.setData({
                    tmplIds: tmplIds,
                    subscribeMessage: subscribeMessage
                });
            }
        });
    }
});